package rest.jbpm.services;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rest.jbpm.JbpmService;
import rest.jbpm.filters.JaxbFilter;

@Path("resources")
public class ResourcesService  extends JbpmService  {

	private static Logger log = LoggerFactory.getLogger(ResourcesService.class);
	
	
	
	@GET
	@Produces(MediaType.TEXT_XML)
	@Path("/xslt/{name}")
	public String getXSLT(@PathParam("name") String name) {
		log.info("trying to get xslt/{}",name);
		try {
			
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			InputStream xslt=JaxbFilter.class.getClassLoader()
			.getResourceAsStream("xslt/"+name);
			IOUtils.copy(xslt, baos);
			return baos.toString("UTF-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}	
	
	@GET
	@Produces("text/css")
	@Path("/css/{name}")
	public String getCSS(@PathParam("name") String name) {
		log.info("trying to get css/{}",name);
		try {
			
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			InputStream css=JaxbFilter.class.getClassLoader()
			.getResourceAsStream("css/"+name);
			IOUtils.copy(css, baos);
			return baos.toString("UTF-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}	
	
	@GET
	@Produces("text/javascript")
	@Path("/js/{name}")
	public String getJS(@PathParam("name") String name) {
		log.info("trying to get js/{}",name);
		try {
			
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			InputStream js=JaxbFilter.class.getClassLoader()
			.getResourceAsStream("js/"+name);
			IOUtils.copy(js, baos);
			return baos.toString("UTF-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}	
}
